package com.project.slsx.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.StorageList1Adapter;
import com.project.slsx.bean.ResultBean;
import com.project.slsx.bean.StorageListBean;
import com.project.slsx.utils.ParamsUtils;

import java.util.List;

/**
 * 点击按钮跳转到入库列表
 */
public class StorageList1Activity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = StorageList1Activity.class.getSimpleName();
    private StorageList1Adapter adapter;
    private TextView number;
    private TextView sumMoney;
    private TextView profit;

    @Override
    public int getLayoutId() {
        return R.layout.activity_storage_list1;
    }

    @Override
    public void initView() {
        TextView title = findViewById(R.id.tv_title);
        title.setText("入库列表");
        findViewById(R.id.rl_back).setOnClickListener(v -> finish());
        findViewById(R.id.btn_storage_addstorage1).setOnClickListener(this);
        number = findViewById(R.id.tv_storage_number);
        sumMoney = findViewById(R.id.tv_storage_sum_money);
        profit = findViewById(R.id.tv_storage_profit);
        RecyclerView rv = findViewById(R.id.recyclerview_storage_list);

        rv.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new StorageList1Adapter(R.layout.layout_item_storage_list);
        rv.setAdapter(adapter);
    }

    @Override
    public void initData() {
        getStorageListData();
    }

    /**
     * 获取库存列表
     */
    private void getStorageListData() {
        HttpParams storagelistParams = ParamsUtils.getStoragelistParams();
        OkGo.<String>post(Constans.BASEURL).tag("storagelist").params(storagelistParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                StorageListBean storageListBean = JSONObject.parseObject(response.body(), StorageListBean.class);
                if (storageListBean != null) {
                    showToast(mContext, storageListBean.getErrmsg());
                    List<StorageListBean.ResultBean> result = storageListBean.getResult();
                    adapter.setNewData(result);
                    setBottomData();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(mContext, "网络错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_storage_addstorage1:
                doStorageConfirm();
                break;
        }
    }

    /**
     * 设置底部数据
     */
    private void setBottomData() {
        double number = 0;
        double sumMoney = 0;
        double profit = 0;
        int size = adapter.getData().size();
        List<StorageListBean.ResultBean> data = adapter.getData();
        for (int i = 0; i < size; i++) {
            number += Double.valueOf(data.get(i).getNumber());
            sumMoney += Double.valueOf(data.get(i).getAmount());
            profit += (double) data.get(i).getProfits();
        }

        this.number.setText(String.valueOf(number));
        this.sumMoney.setText(String.valueOf(sumMoney));
        this.profit.setText(String.valueOf(profit));
    }

    /**
     * 确认入库
     */
    private void doStorageConfirm() {
        HttpParams storageConfirm = ParamsUtils.getStorageConfirm();
        OkGo.<String>post(Constans.BASEURL).tag("getStorageConfirm").params(storageConfirm).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                ResultBean resultBean = JSONObject.parseObject(response.body(), ResultBean.class);
                showToast(mContext, resultBean.getErrmsg());
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(mContext, "网络错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    }
}
